<?php
/**
 * @package qBase
 * @subpackage model
 *
 */

/**
 * QDbStorageInfo
 * 
 * @package qBase
 * @subpackage model
 */
class QDbStorageInfo
{
	/**
	 * The load info
	 * The key is the name of the property to set
	 * The first parameter in the array is the name of the table
	 * The second parameter is the list of columns to load
	 * The third specifies the list foreign keys (column => table)
	 * The fourth is the virtual collection where info should be added
	 *
	 * @var array
	 */
	/*
	private static $LoadInfoData = array(
			"databases" => array("Databases", array("id", "name"), null, null),
			"tables" => array("Tables", array("id", "id_database", "name"), array("id_database" => "databases"), array("databases" => "id_database")),
			"fields" => array("Fields", array("id", "id_table", "name", "type"), array("id_table" => "tables"), array("tables" => "id_table")),
	
			"objects" => array("CodeObjects", array("id", "class", "id_parent", "is_qidata"), array("id_parent" => "objects"), null),
			"properties" => array("CodeProperties", array("id", "id_class", "id_type", "name", "inherited", "access"), array("id_class" => "objects", "id_type" => "objects"), array("id_class" => "objects")),
	
			"map_objects" => array("MapObjects", array("id", "id_object", "id_table", "name"), array("id_object" => "objects", "id_table" => "tables"), null),
			"map_properties" => array("MapProperties", 
						array("id", "id_map_object", "id_property", "id_field", 
							"name", "id_fk_index", "is_collection", "id_collection_table", 
							"id_collection_fk_owner", "id_collection_fk_owner")),
			"map_indexes" => array("MapIndexes", array("id", "id_table", "name", "type", "id_foreign_key_to"))
		);
	*/
	
	private static $LoadInfoData = array(
			"databases" => array("Databases", array("id", "name")),
			"tables" => array("Tables", array("id", "id_database", "name")),
			"fields" => array("Fields", array("id", "id_table", "name", "type")),
	
			"objects" => array("CodeObjects", array("id", "class", "id_parent", "is_qidata")),
			"properties" => array("CodeProperties", array("id", "id_class", "id_type", "name", "inherited", "access")),
	
			"map_objects" => array("MapObjects", array("id", "id_object", "id_table", "name")),
			"map_properties" => array("MapProperties", 
						array("id", "id_map_object", "id_property", "id_field", 
							"name", "id_fk_index", "is_collection", "id_collection_table", 
							"id_collection_fk_owner", "id_collection_fk_owner")),
			"map_indexes" => array("MapIndexes", array("id", "id_table", "name", "type", "id_foreign_key_to"))
		);

	public $databases;
	public $tables;
	public $fields;

	public $objects;
	public $properties;

	public $map_objects;
	public $map_properties;
	public $map_indexes;

	/**
	 * Storage
	 *
	 * @var QDbStorage
	 */
	private $storage;
	
	public function __construct(QDbStorage $storage)
	{
		$this->storage = $storage;
		$this->unsetInfo();
	}
	
	public function getDatabaseId($name)
	{
		foreach ($this->databases as $id => $obj)
			if ($obj->name == $name)
				return $id;
	}
	
	public function getDatabaseName($id)
	{
		return $this->databases[$id]->name;
	}
	
	public function getTableName($id)
	{
		return $this->tables[$id]->name;
	}
	
	public function getTableId($name, $db = null)
	{
		if (!$db)
			$db = $this->storage->getDefaultDatabaseId();
		else 
			$db = $this->getDatabaseId($db);
			
		// var_dump($db);
			
		foreach ($this->tables as $id => $obj)
			if (($obj->id_database == $db) && ($obj->name == $name))
				return $id;
		
		return null;
	}

	public function getObjectName($id)
	{
		return $this->objects[$id]->name;
	}
	
	public function getObjectId($name)
	{
		foreach ($this->objects as $id => $obj)
			if ($obj->class == $name)
				return $id;
	}

	public function getFieldName($id)
	{
		return $this->fields[$id]->name;
	}
	
	public function getMapObjectId($name)
	{
		foreach ($this->map_objects as $id => $obj)
			if ($obj->name == $name)
				return $id;
		return null;
	}
	
	public function getFieldId($table, $field, $db = null)
	{
		$table_id = $this->getTableId($table, $db);
		
		foreach ($this->fields as $id => $obj)
			if (($obj->name == $field) && ($table_id == $obj->id_table))
				return $id;
				
		return null;
	}

	public function loadInfo($full = false, $unset = false)
	{
		if ($unset)
			$this->unsetInfo(true);
		
		if ($full)
		{
			
		}
		else 
		{
			// Load databases, tables, objects, map_objects, the rest load as needed
			foreach (QDbStorageInfo::$LoadInfoData as $key => $val)
			{
				$the_table = $this->storage->getFrameTable($val[0]);
				$the_columns = $val[1];
				
				$q = QSqlQuery::NewQuery()->select();
				foreach ($the_columns as $f)
					$q->{$f};
				$q->from()->{$the_table};
				
				$result = $q->executeQuery($this->storage->getConnection());
				
				while ($obj = $this->storage->getConnection()->fetchObject($result))
				{
					$this->{$key}[$obj->id] = $obj;
				}
			}
		}
	}
	
	public function refresh()
	{
		$this->loadInfo(false, true);
	}
	
	public function unsetInfo($unset = false)
	{
		foreach (QDbStorageInfo::$LoadInfoData as $key => $val)
		{
			if ($unset)
				unset($this->{$key});
			$this->{$key} = array();
		}
	}
}

?>